package com.langyue.recordmanage.controller;

import com.langyue.recordmanage.mapper.UserMapper;
import com.langyue.recordmanage.mapper.UserVoMapper;
import com.langyue.recordmanage.model.vo.UserVo;
import com.langyue.recordmanage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 测试接口
 * @Author: wanglang
 * @CreateTime: 2022/4/1
 * @Company:
 */
@RestController
public class TestController {
    @Autowired
    private UserVoMapper userVoMapper;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/testAjax/{id}",method = RequestMethod.DELETE)
    public String testAjax(@PathVariable String id, String name, String password){
        return name+password;
    }

    @RequestMapping(value = "/test/queryById",method = RequestMethod.GET)
    public List<UserVo> queryById(Integer pageIndex, Integer pageSize){
        Integer pageOffset = (pageIndex-1)*pageSize;
        return userVoMapper.selectUserVoById(pageOffset,pageSize);
    }

    @RequestMapping("testJenkins")
    public String testJenkins(){
        return "success";
    }

    @Transactional
    @RequestMapping("/testTransactional")
    public void testTransactional(){
        test();
    }

    @Transactional
    public void test(){
        try {
            User user = new User();
            user.setUserName("1222");
            userMapper.insert(user);
            int x = 1/0;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    // @Transactional注解只能在抛出RuntimeException或者Error时才会触发事务的回滚，常见的非RuntimeException是不会触发事务的回滚的。但是我们平时做业务处理时，需要捕获异常，所以可以手动抛出RuntimeException异常或者添加rollbackFor = Exception.class(也可以指定相应异常)

    /**
     * 只有public修饰的方法才会生效
     * 情况一：都有事务注解，异常在子方法出现，事务生效
     * 情况二：都有事务注解，异常在主方法出现，事务生效
     * 情况三：只有主方法有事务注解，异常在子方法出现，事务生效
     * 情况四：只有主方法有事务注解，异常在主方法出现，事务生效
     * 情况五：只有子方法有事务注解，异常在子方法出现，事务不生效
     **/

}
